package com.coincalf.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.dto.WithdrawAddressDTO;
import com.coincalf.entity.UserWallet;
import com.coincalf.framework.exception.GlobalDefaultException;
import com.coincalf.framework.http.Response;
import com.coincalf.framework.security.UserDetails;
import com.coincalf.framework.utils.AddressUtil;
import com.coincalf.service.UserWalletService;
import com.coincalf.user.dto.UserDTO;
import com.coincalf.user.feign.UserServiceClient;
import com.coincalf.vo.DeleteWalletForm;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @Description: 用户提币钱包地址
 * @Author: Chen Long
 * @Date: Created in 2018/5/22 下午3:04
 * @Modified by: Chen Long
 */
@RestController
@RequestMapping("/user/wallet")
@Api(value = "提币钱包地址", description = "提币钱包地址 REST API")
public class UserWalletController {

    @Autowired
    private UserWalletService userWalletService;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private CommonController commonController;

    /**
     * 添加提币钱包地址
     *
     * @param withdrawAddress 添加提币地址请求参数
     * @return
     */
    //@PreAuthorize("isAuthenticated()")
    @PostMapping("/address")
    @ApiOperation(value = "WITHDRAW-ADDRESS-001 添加提币地址", notes = "添加提币地址", httpMethod = "POST")
    @ApiImplicitParam(name = "withdrawAddress", value = "添加提币地址请求参数", required = true, dataType = "WithdrawAddressDTO", paramType = "body")
    public Object addAddress(HttpServletRequest request, @RequestBody WithdrawAddressDTO withdrawAddress) {
        Long userId = commonController.getUserId(request);
        boolean b = AddressUtil.verifyAddress(withdrawAddress.getCoinId(), withdrawAddress.getAddress());
        if (!b) {
            return Response.err(-99, "地址格式错误");
        }
        userWalletService.addAddress(withdrawAddress, userId);
        return Response.ok();
    }

    /**
     * 删除提币钱包地址
     *
     * @return
     */
    //@PreAuthorize("isAuthenticated()")
    @PostMapping("/deleteAddress")
    @ApiOperation(value = "删除钱包地址", notes = "删除钱包地址", httpMethod = "POST")
    public Object deleteAddress(HttpServletRequest request, @RequestBody DeleteWalletForm form) {
        Long userId = commonController.getUserId(request);
        /*UserDTO userDTO = userServiceClient.selectById(userId);
        if (!new BCryptPasswordEncoder().matches(form.getPayPassword(), userDTO.getPaypassword())) {
            throw new GlobalDefaultException(50020);
        }*/
        boolean flag = userWalletService.deleteById(form.getAddressId());
        if (!flag) {
            throw new GlobalDefaultException(20009);
        }
        return Response.ok();
    }

    //@PreAuthorize("isAuthenticated()")
    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation(value = "用户的提币地址", httpMethod = "GET")
    public Response selectUserWalletList(HttpServletRequest request, @PathVariable long coinId) {
        Long userId = commonController.getUserId(request);
        EntityWrapper<UserWallet> e = new EntityWrapper<>();
        e.eq("user_id", userId);
        if (coinId != -1) {
            e.eq("coin_id", coinId);
        }
        return Response.ok(userWalletService.selectList(e));
    }

}
